package cn.iocoder.an.module.mold.service.upkeeptask2item;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.an.module.mold.controller.admin.upkeeptask2item.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.upkeeptask2item.UpkeepTask2itemDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.mold.dal.mysql.upkeeptask2item.UpkeepTask2itemMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.mold.enums.ErrorCodeConstants.*;

/**
 * 模具保养任务以及保养项目对照 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class UpkeepTask2itemServiceImpl implements UpkeepTask2itemService {

    @Resource
    private UpkeepTask2itemMapper upkeepTask2itemMapper;

    @Override
    public Long createUpkeepTask2item(UpkeepTask2itemSaveReqVO createReqVO) {
        // 插入
        UpkeepTask2itemDO upkeepTask2item = BeanUtils.toBean(createReqVO, UpkeepTask2itemDO.class);
        upkeepTask2itemMapper.insert(upkeepTask2item);

        // 返回
        return upkeepTask2item.getId();
    }

    @Override
    public void updateUpkeepTask2item(UpkeepTask2itemSaveReqVO updateReqVO) {
        // 校验存在
        validateUpkeepTask2itemExists(updateReqVO.getId());
        // 更新
        UpkeepTask2itemDO updateObj = BeanUtils.toBean(updateReqVO, UpkeepTask2itemDO.class);
        upkeepTask2itemMapper.updateById(updateObj);
    }

    @Override
    public void deleteUpkeepTask2item(Long id) {
        // 校验存在
        validateUpkeepTask2itemExists(id);
        // 删除
        upkeepTask2itemMapper.deleteById(id);
    }

    @Override
    public void deleteUpkeepTask2itemListByIds(List<Long> ids) {
        // 删除
        upkeepTask2itemMapper.deleteByIds(ids);
    }


    private void validateUpkeepTask2itemExists(Long id) {
        if (upkeepTask2itemMapper.selectById(id) == null) {
            throw exception(UPKEEP_TASK2ITEM_NOT_EXISTS);
        }
    }

    @Override
    public UpkeepTask2itemDO getUpkeepTask2item(Long id) {
        return upkeepTask2itemMapper.selectById(id);
    }

    @Override
    public PageResult<UpkeepTask2itemDO> getUpkeepTask2itemPage(UpkeepTask2itemPageReqVO pageReqVO) {
        return upkeepTask2itemMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UpkeepTask2itemDO> getUpkeepTask2itemListByTaskId(Long taskId) {
        return upkeepTask2itemMapper.selectList(new LambdaQueryWrapper<UpkeepTask2itemDO>()
                .eq(UpkeepTask2itemDO::getTaskId, taskId));
    }


}